package com.example.li.day8_15;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Wlecome extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        intiView();
    }

    private void intiView() {
        tv_show = findViewById(R.id.tv_show);
        h.sendMessage(h.obtainMessage(TXEP_SUCCEED,3,0));
        tv_show.setOnClickListener(this);
    }

    private final static int TXEP_SUCCEED=0X333;
    private final static int TXEP_FAILURE=0x234;

    private Handler h=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch(msg.what){
                case TXEP_SUCCEED:
                    if (msg.arg1<0){
                        h.sendEmptyMessage(TXEP_FAILURE);
                    }else{
                        tv_show.setText(msg.arg1+"s 后关闭");
                        h.sendMessageDelayed(h.obtainMessage(TXEP_SUCCEED,msg.arg1-1,0),1500);
                    }
                    break;
                case TXEP_FAILURE:
                    goMain();
                    break;
            }
            return false;
        }

        private void goMain() {
            h.removeMessages(TXEP_SUCCEED);
            h.removeMessages(TXEP_FAILURE);
            startActivity(new Intent(Wlecome.this,LoginActivity.class));
            finish();
        }
    });

    @Override
    public void onClick(View v) {
       h.sendMessage(h.obtainMessage(TXEP_FAILURE));
    }
}
